//
//  UIView+ChainExt.swift
//  QJDecoratingHeadlines
//
//  Created by hudan on 2023/7/4.
//  Copyright © 2023 www.jia.com. All rights reserved.
//

import Foundation

//MARK: - UIKit
//MARK: - UIView
extension UIView {
    @discardableResult
    func extHidden(hidden: Bool) -> Self {
        self.isHidden = hidden
        return self
    }
    
    @discardableResult
    func extAlpha(alpha: CGFloat) -> Self {
        self.alpha = alpha
        return self
    }
    
    @discardableResult
    func extBgColor(color: UIColor?) -> Self {
        self.backgroundColor = color
        return self
    }
    
    @discardableResult
    func extUserInteractionEnabled(enable: Bool) -> Self {
        self.isUserInteractionEnabled = enable
        return self
    }
    
    @discardableResult
    func extTag(tag: Int) -> Self {
        self.tag = tag
        return self
    }
    
    //MARK: - Layout
    @discardableResult
    func extFrame(frame: CGRect) -> Self {
        self.frame = frame
        return self
    }
    
    @discardableResult
    func extCenter(center: CGPoint) -> Self {
        self.center = center
        return self
    }
    
    @discardableResult
    func extBounds(bounds: CGRect) -> Self {
        self.bounds = bounds
        return self
    }
    
    @discardableResult
    func extFrameX(x: CGFloat) -> Self {
        self.ks_x = x
        return self
    }
    
    @discardableResult
    func extFrameY(y: CGFloat) -> Self {
        self.ks_y = y
        return self
    }
    
    @discardableResult
    func extFrameSize(size: CGSize) -> Self {
        self.ks_size = size
        return self
    }
    
    @discardableResult
    func extOrigin(point: CGPoint) -> Self {
        self.ks_point = point
        return self
    }
    
    @discardableResult
    func extLeft(left: CGFloat) -> Self {
        self.ks_x = left
        return self
    }
    
    @discardableResult
    func extRight(right: CGFloat) -> Self {
        self.ks_maxX = right
        return self
    }
    
    @discardableResult
    func extHeight(height: CGFloat) -> Self {
        self.ks_height = height
        return self
    }
    
    @discardableResult
    func extWidth(width: CGFloat) -> Self {
        self.ks_width = width
        return self
    }
    
    @discardableResult
    func extCenterX(x: CGFloat) -> Self {
        self.ks_centerX = x
        return self
    }
    
    @discardableResult
    func extCenterY(y: CGFloat) -> Self {
        self.ks_centerY = y
        return self
    }
    
    @discardableResult
    func extBottom(bottom: CGFloat) -> Self {
        self.ks_maxY = bottom
        return self
    }
    
    //MARK: - Layer
    @discardableResult
    func extClipsToBounds(bounds: Bool) -> Self {
        self.clipsToBounds = bounds
        return self
    }
    
    @discardableResult
    func extRadius(radius: CGFloat) -> Self {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
        return self
    }
    
    @discardableResult
    func extMaskBounds(mask: Bool) -> Self {
        self.layer.masksToBounds = mask
        return self
    }
    
    @discardableResult
    func extBorderW(width: CGFloat) -> Self {
        self.layer.borderWidth = width
        return self
    }
    
    @discardableResult
    func extBorderColor(color: UIColor?) -> Self {
        self.layer.borderColor = color?.cgColor
        return self
    }
}

//MARK: - UIButton
extension UIButton {
    @discardableResult
    func extTitle(title: String?) -> Self {
        self.setTitle(title, for: .normal)
        return self
    }
    
    @discardableResult
    func extSelectTitle(title: String?) -> Self {
        self.setTitle(title, for: .selected)
        return self
    }
    
    @discardableResult
    func extTitleColor(color: UIColor?) -> Self {
        self.setTitleColor(color, for: .normal)
        return self
    }
    
    @discardableResult
    func extSelectTitleColor(color: UIColor?) -> Self {
        self.setTitleColor(color, for: .selected)
        return self
    }
    
    @discardableResult
    func extImage(img: UIImage?) -> Self {
        self.setImage(img, for: .normal)
        return self
    }
    
    @discardableResult
    func extSelectImage(img: UIImage?) -> Self {
        self.setImage(img, for: .selected)
        return self
    }
    
    @discardableResult
    func extFont(font: UIFont?) -> Self {
        self.titleLabel?.font = font
        return self
    }
}

//MARK: - UILabel
extension UILabel {
    @discardableResult
    func extText(text: String?) -> Self {
        self.text = text
        return self
    }
    
    @discardableResult
    func extTextColor(color: UIColor?) -> Self {
        self.textColor = color
        return self
    }
    
    @discardableResult
    func extTextAligment(aligment: NSTextAlignment) -> Self {
        self.textAlignment = aligment
        return self
    }
    
    @discardableResult
    func extFont(font: UIFont?) -> Self {
        self.font = font
        return self
    }
    
    @discardableResult
    func extNumberOfLines(lines: Int) -> Self {
        self.numberOfLines = lines
        return self
    }
}

//MARK: - UITextField
extension UITextField {
    @discardableResult
    func extPlaceholder(text: String?) -> Self {
        self.placeholder = text
        return self
    }
    
    @discardableResult
    func extAttributePlaceholder(text: NSAttributedString?) -> Self {
        self.attributedPlaceholder = text
        return self
    }
    
    @discardableResult
    func extReturnType(type: UIReturnKeyType) -> Self {
        self.returnKeyType = type
        return self
    }
    
    @discardableResult
    func extClearMode(mode: UITextField.ViewMode) -> Self {
        self.clearButtonMode = .always
        return self
    }
    
    @discardableResult
    func extLeftMargin(margin: CGFloat) -> Self {
        self.leftView = UIView()
        self.leftViewMode = .always
        return self
    }
    
    @discardableResult
    func extRightMargin(margin: CGFloat) -> Self {
        self.rightView = UIView()
        self.rightViewMode = .always
        return self
    }
}

extension UIImageView {
    @discardableResult
    func extImage(img: UIImage?) -> Self {
        self.image = img
        return self
    }
    
    @discardableResult
    func extContentMode(mode: UIView.ContentMode) -> Self {
        self.contentMode = mode
        return self
    }
}
